package org.example;

import java.util.ArrayList;
import java.util.List;

class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }

public class Test1 {
    //leetcode 257 二叉树的所有路径 https://leetcode.cn/problems/binary-tree-paths/description/
    public List<String> list1;
    public List<String> binaryTreePaths1(TreeNode root) {
        list1 = new ArrayList<>();
        dfs1(root,new StringBuilder());
        return list;
    }

    public void dfs1(TreeNode root,StringBuilder s) {
        StringBuilder sb = new StringBuilder(s);
        sb.append(root.val);
        if (root.left == null && root.right == null) {
            list.add(sb.toString());
            return;
        }
        sb.append("->");
        if (root.left != null) {
            dfs1(root.left,sb);
        }
        if (root.right != null) {
            dfs1(root.right,sb);
        }
    }

    public List<String> list;
    StringBuilder s;
    public List<String> binaryTreePaths(TreeNode root) {
        list = new ArrayList<>();
        s = new StringBuilder();
        dfs(root);
        return list;
    }

    public void dfs(TreeNode root) {
        s.append(root.val);
        if (root.left == null && root.right == null) {
            list.add(s.toString());
            return;
        }
        if (root.left != null) {
            s.append("->");
            dfs(root.left);
            s.delete(s.length() - 3, s.length());
        }
        if (root.right != null) {
            s.append("->");
            dfs(root.right);
            s.delete(s.length() - 3, s.length());
        }
    }

    public static void main(String[] args) {
        TreeNode a = new TreeNode(1);
        TreeNode b = new TreeNode(2);
        TreeNode c = new TreeNode(3);
        TreeNode d = new TreeNode(5);
        a.left = b;
        a.right = c;
        b.right = d;
        Test1 test1 = new Test1();
        test1.binaryTreePaths(a);
        System.out.println(333);
    }
}
